//
//  BRTagView.m
//  FD
//
//  Created by Yasin on 2018/10/23.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  要有线

#import "BRTagView.h"
@interface BRTagView()
/**0.0背景view
 self 灰色
 bgView 白色, 上面留10px 作为线
 */
@property (nonatomic, strong) UIView *bgview;

/**1.我的标签*/
@property (nonatomic,  strong) UILabel *myTagLb;
/**标记最后一个下标*/
@property (nonatomic,  strong) UILabel *lastLb;
/**箭头*/
@property (nonatomic, strong) UIImageView *arrowIv;
/**tagLb数组*/
@property (nonatomic, strong) NSMutableArray *tagLbArrM;
@end

@implementation BRTagView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        self.backgroundColor = kMainLightGrayColor;
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.myTagLb];
    
    //self 灰色
    //bgView 白色 ,所有的子控件都加在bgView上,
    CGFloat s_h = self.bounds.size.height;
    CGFloat s_w = self.bounds.size.width;
    
    
    self.bgview.frame = CGRectMake(0, 10, s_w, s_h - 10);
    [self addSubview:self.bgview];
    
    CGFloat bgview_h = self.bgview.bounds.size.height;
    CGFloat y = (bgview_h - 30)*0.5;
    self.myTagLb.frame = CGRectMake(20, y, 100, 30);
    [self.bgview addSubview:self.myTagLb];
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self addGestureRecognizer:tap];
    
    
    
    //右边箭头 >
    [self.bgview addSubview:self.arrowIv];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.arrowIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(self.bgview.mas_centerY);
        make.right.equalTo(self.bgview.mas_right).offset(-10);
    }];
}

#pragma mark - 点击事件
- (void)tapGesture:(UITapGestureRecognizer *)tap {
    if (self.clickMyTagViewBlock) {
        self.clickMyTagViewBlock();
    }
}


#pragma mark - 更新标签
/**
 需求:
 1.只有三个
 2.宽度自适应
 3.只作赋值不允许重复创建
 */

- (void)updateTags:(NSArray *)tags {
    
    //bug:
    if (tags.count) {
        //记录已经添加的,全部移除。
        for (UILabel *lb in self.tagLbArrM) {
            [lb removeFromSuperview];
        }
    }
    
    CGFloat bgview_h = self.bgview.bounds.size.height;
    CGFloat s_w = self.bounds.size.width;
    CGFloat y = (bgview_h - 30)*0.5;
    
    //如果数组为空 就不显示标签、
    if(tags.count == 0) return;
    
    CGFloat left  = self.myTagLb.rb_right;
    
    self.lastLb = nil; //初始化为 nil
    
    for (int i=0; i<tags.count; i++) {
        UILabel *lb = [[UILabel alloc] init];
        
        //tags是数组<@{id = 23; name = "\U83f2\U529b\U725b\U6392";}>
        
        NSDictionary *dic = tags[i];
        
        lb.text= dic[@"name"];
        lb.textAlignment = NSTextAlignmentCenter;
        lb.font = [UIFont systemFontOfSize:14.0*kScaleFit];
        lb.layer.masksToBounds = YES;
        lb.layer.cornerRadius = 10; //圆角弧度12px
        lb.layer.borderColor = [UIColor lightGrayColor].CGColor;
        lb.layer.borderWidth = 1.0;
        lb.textColor = kLightGrayTextColor; //灰色的
        
        //计算宽度
        CGSize lbSize = [lb.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0*kScaleFit]}];
        CGFloat lb_w = lbSize.width *1.5; //放大1.3倍
        CGFloat lb_h = 30;
        CGFloat maxRight = 0;
        //先确定第一个frame
        if (i == 0) {
            lb.frame = CGRectMake(left, y, lb_w, lb_h);
        }else {
            
            //用于判断是否超出边界。
            maxRight = self.lastLb.rb_right + 10 + lb_w;
            
            lb.frame = CGRectMake(self.lastLb.rb_right + 10, y, lb_w, lb_h);
        }
        
        //思路就是:没一个文本的坐标都依赖上一个来确定
        self.lastLb = lb;
        
        if (maxRight < s_w) { //超过一行就不显示了。
            //只显示3个
            [self.tagLbArrM addObject:lb];
            [self.bgview addSubview:lb];
        }
    }
}



//- (void)updateTags:(NSArray *)tags {
//
//    CGFloat s_h = self.bounds.size.height;
//    CGFloat s_w = self.bounds.size.width;
//    CGFloat y = (s_h - 30)*0.5;
//
//    //如果数组为空 就不显示标签、
//    if(tags.count == 0) return;
//
//    CGFloat left  = self.myTagLb.rb_right;
//
//
//    self.lastLb = nil; //初始化为 nil
//
//    for (int i=0; i<tags.count; i++) {
//        UILabel *lb = [[UILabel alloc] init];
//        //tags是数组<@{id = 23; name = "\U83f2\U529b\U725b\U6392";}>
//
//        NSDictionary *dic = tags[i];
//
//        lb.text= dic[@"name"];
//        lb.textAlignment = NSTextAlignmentCenter;
//        lb.font = [UIFont systemFontOfSize:14.0*kScaleFit];
//        lb.layer.masksToBounds = YES;
//        lb.layer.cornerRadius = 5;
//        lb.layer.borderColor = [UIColor lightGrayColor].CGColor;
//        lb.layer.borderWidth = 1.0;
//
//        //计算宽度
//        CGSize lbSize = [lb.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0*kScaleFit]}];
//        CGFloat lb_w = lbSize.width *1.3; //放大1.3倍
//        CGFloat lb_h = 30;
//        CGFloat maxRight = 0;
//        //先确定第一个frame
//        if (i == 0) {
//            lb.frame = CGRectMake(left, y, lb_w, lb_h);
//        }else {
//
//            //用于判断是否超出边界。
//            maxRight = self.lastLb.rb_right + 10 + lb_w;
//
//            lb.frame = CGRectMake(self.lastLb.rb_right + 10, y, lb_w, lb_h);
//        }
//
//        //思路就是:没一个文本的坐标都依赖上一个来确定
//        self.lastLb = lb;
//
//        if (maxRight < s_w) { //超过一行就不显示了。
//            //只显示3个
//            [self addSubview:lb];
//        }
//    }
//}

//随机色
- (UIColor *)rb_randomColor {
    int r = (arc4random()%256);
    int g = (arc4random()%256);
    int b = (arc4random()%256);
    return [UIColor colorWithRed: r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];
}


#pragma mark - getter
- (UIView *)bgview {
    if (!_bgview) {
        _bgview = [UIView new];
        _bgview.backgroundColor = [UIColor whiteColor];
    }
    return _bgview;
}

- (UILabel *)myTagLb {
    if(!_myTagLb) {
        _myTagLb = [UILabel new];
        //国际化
        NSString *preferenceSTr = NSLocalizedString(PreferenceTas, @"偏好标签");
        _myTagLb.text = preferenceSTr;
        
        _myTagLb.backgroundColor = [UIColor clearColor];
        _myTagLb.textColor = BR_RGB_HEX(0x464646, 1.0);
        _myTagLb.font = [UIFont systemFontOfSize:16.0f * kScaleFit];
        _myTagLb.textAlignment = NSTextAlignmentLeft;
    }
    return _myTagLb;
}

- (UIImageView *)arrowIv {
    if (!_arrowIv) {
        _arrowIv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"右箭头.png"]];
    }
    return _arrowIv;
}

- (NSMutableArray *)tagLbArrM {
    if (!_tagLbArrM) {
        _tagLbArrM = [NSMutableArray array];
    }
    return _tagLbArrM;
}

@end
